﻿/* EntitiesToDTOs. Copyright (c) 2011. Fabian Fernandez.
 * http://entitiestodtos.codeplex.com
 * Licensed by Common Development and Distribution License (CDDL).
 * http://entitiestodtos.codeplex.com/license
 * Fabian Fernandez. 
 * http://www.linkedin.com/in/fabianfernandezb/en
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using EntitiesToDTOs.Properties;

namespace EntitiesToDTOs.Helpers
{
    /// <summary>
    /// Manages the Log operations.
    /// </summary>
    internal class LogManager
    {
        /// <summary>
        /// Indicates if the Log process is active.
        /// </summary>
        public static bool IsLogActive
        {
            get
            {
                return _isLogActive;
            }
            set
            {
                _isLogActive = value;
            }
        }
        private static bool _isLogActive = false;
        

        /// <summary>
        /// Logs an Error message
        /// </summary>
        /// <param name="logMessage"></param>
        public static void LogError(string logMessage)
        {
            LogManager.Log(string.Format(Resources.LogError,
                DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()) + Environment.NewLine + 
                logMessage);
        }

        /// <summary>
        /// Logs an Info message
        /// </summary>
        /// <param name="logMessage"></param>
        public static void LogInfo(string logMessage)
        {
            LogManager.Log(string.Format(Resources.LogInfo,
                DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()) + Environment.NewLine + 
                logMessage);
        }

        /// <summary>
        /// Logs a message
        /// </summary>
        /// <param name="logMessage"></param>
        private static void Log(string logMessage)
        {
            if (LogManager.IsLogActive)
            {
                var worker = new BackgroundWorker();
                worker.WorkerReportsProgress = false;

                worker.DoWork += new DoWorkEventHandler(LogManager.Log_DoWork);

                worker.RunWorkerAsync(logMessage);
            }
        }

        /// <summary>
        /// Executed when the BackgroundWorker decides to execute the provided Logic (i.e., the Log process).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        static void Log_DoWork(object sender, DoWorkEventArgs args)
        {
            try
            {
                var logMessage = (string)args.Argument;

                File.AppendAllText(Resources.LogFilePath, logMessage +
                    Environment.NewLine + Resources.LogSeparator + Environment.NewLine);
            }
            catch (Exception)
            {
                // Do nothing...
            }
        }
    }
}